#!/bin/bash

# 默认设置
DEFAULT_PROXY_HOST="127.0.0.1"
DEFAULT_PROXY_PORT="8080"
CONFIG_FILE="config.yaml"
# networksetup -listnetworkserviceorder
# 根据实际情况修改， 
DEFAULT_NETWORK_SERVICE="Wi-Fi"

# 从配置文件读取代理配置
function get_proxy_config() {
    if [ -f "$CONFIG_FILE" ]; then
        # 尝试从配置文件读取代理配置
        PROXY_HOST=$(python3 -c "
import yaml
try:
    with open('$CONFIG_FILE', 'r') as f:
        config = yaml.safe_load(f)
        print(config.get('server', {}).get('system_proxy', {}).get('host', '$DEFAULT_PROXY_HOST'))
except:
    print('$DEFAULT_PROXY_HOST')
" 2>/dev/null)
        
        PROXY_PORT=$(python3 -c "
import yaml
try:
    with open('$CONFIG_FILE', 'r') as f:
        config = yaml.safe_load(f)
        print(config.get('server', {}).get('system_proxy', {}).get('port', '$DEFAULT_PROXY_PORT'))
except:
    print('$DEFAULT_PROXY_PORT')
" 2>/dev/null)
        
        # 如果读取失败，使用默认值
        if [ -z "$PROXY_HOST" ] || [ "$PROXY_HOST" = "None" ]; then
            PROXY_HOST=$DEFAULT_PROXY_HOST
        fi
        if [ -z "$PROXY_PORT" ] || [ "$PROXY_PORT" = "None" ]; then
            PROXY_PORT=$DEFAULT_PROXY_PORT
        fi
    else
        PROXY_HOST=$DEFAULT_PROXY_HOST
        PROXY_PORT=$DEFAULT_PROXY_PORT
    fi
}

function show_proxy() {
    local service=${1:-$DEFAULT_NETWORK_SERVICE}
    echo "当前 HTTP 代理设置："
    networksetup -getwebproxy "$service"
    echo -e "\n当前 HTTPS 代理设置："
    networksetup -getsecurewebproxy "$service"
}

function enable_proxy() {
    # 如果没有指定参数，从配置文件读取
    if [ -z "$1" ] && [ -z "$2" ]; then
        get_proxy_config
        local host=${1:-$PROXY_HOST}
        local port=${2:-$PROXY_PORT}
    else
        local host=${1:-$DEFAULT_PROXY_HOST}
        local port=${2:-$DEFAULT_PROXY_PORT}
    fi
    local service=${3:-$DEFAULT_NETWORK_SERVICE}
    local type=${4:-"all"}  # all, http, https
    
    echo "正在设置代理... ($service -> $host:$port)"
    
    case "$type" in
        "http")
            networksetup -setwebproxy "$service" "$host" "$port"
            networksetup -setwebproxystate "$service" on
            ;;
        "https")
            networksetup -setsecurewebproxy "$service" "$host" "$port"
            networksetup -setsecurewebproxystate "$service" on
            ;;
        "all")
            networksetup -setwebproxy "$service" "$host" "$port"
            networksetup -setsecurewebproxy "$service" "$host" "$port"
            networksetup -setwebproxystate "$service" on
            networksetup -setsecurewebproxystate "$service" on
            ;;
    esac
    
    echo "代理已启用"
    show_proxy "$service"
}

function disable_proxy() {
    local service=${1:-$DEFAULT_NETWORK_SERVICE}
    local type=${2:-"all"}  # all, http, https
    
    echo "正在关闭代理... ($service)"
    
    case "$type" in
        "http")
            networksetup -setwebproxystate "$service" off
            ;;
        "https")
            networksetup -setsecurewebproxystate "$service" off
            ;;
        "all")
            networksetup -setwebproxystate "$service" off
            networksetup -setsecurewebproxystate "$service" off
            ;;
    esac
    
    echo "代理已关闭"
    show_proxy "$service"
}

function list_networks() {
    echo "可用的网络服务："
    networksetup -listallnetworkservices
}

# 显示帮助信息
function show_help() {
    echo "使用方法："
    echo "  查看代理设置："
    echo "    ./proxy_helper.sh show [网络服务名]"
    echo ""
    echo "  启用代理："
    echo "    ./proxy_helper.sh on [主机名] [端口] [网络服务名] [类型]"
    echo "    类型可以是: all(默认), http, https"
    echo "    示例："
    echo "      ./proxy_helper.sh on                     # 使用默认设置"
    echo "      ./proxy_helper.sh on 127.0.0.1 8080     # 指定主机和端口"
    echo "      ./proxy_helper.sh on 127.0.0.1 8080 Wi-Fi http  # 只设置 HTTP 代理"
    echo ""
    echo "  关闭代理："
    echo "    ./proxy_helper.sh off [网络服务名] [类型]"
    echo "    示例："
    echo "      ./proxy_helper.sh off                # 关闭所有代理"
    echo "      ./proxy_helper.sh off Wi-Fi https   # 只关闭 HTTPS 代理"
    echo ""
    echo "  列出网络服务："
    echo "    ./proxy_helper.sh list"
    echo ""
    echo "默认设置："
    echo "  主机: $DEFAULT_PROXY_HOST"
    echo "  端口: $DEFAULT_PROXY_PORT"
    echo "  网络服务: $DEFAULT_NETWORK_SERVICE"
    echo "  配置文件: $CONFIG_FILE"
    echo ""
    echo "注意：如果存在配置文件，将自动从配置文件读取代理设置"
}

case "$1" in
    "show")
        show_proxy "$2"
        ;;
    "on")
        enable_proxy "$2" "$3" "$4" "$5"
        ;;
    "off")
        disable_proxy "$2" "$3"
        ;;
    "list")
        list_networks
        ;;
    *)
        show_help
        ;;
esac 